*partisan identification (1 = strong democrat; 7 = strong republican)
gen pid = . 
replace pid = 1 if Q2 == 1 & Q3 == 1
replace pid = 2 if Q2 == 1 & Q3 == 2
replace pid = 3 if Q2 > 2 & Q5 == 1
replace pid = 4 if Q2 > 2 & Q5 == 2
replace pid = 5 if Q2 > 2 & Q5 == 3
replace pid = 6 if Q2 == 2 & Q4 == 2
replace pid = 7 if Q2 == 2 & Q4 == 1


*create treatment condition dummy variables
gen persp = 0
replace persp = 1 if condition == "PT RD" | condition == "PT RR"

gen info = 0
replace info = 1 if condition == "IT RD" | condition == "IT RR"

gen control = 0
replace control = 1 if condition == "control RD" | condition == "control RR"

gen dem_condition = 0
replace dem_condition = 1 if party == "Democrat"

gen rep_condition = 0
replace rep_condition = 1 if party == "Republican"

*renaming duplicate variable names, and creating other variables
rename Z know_1
rename AA know_2
rename AB know_3
rename AC know_4
rename Q6 know_5

rename AJ rd_know_dem
rename AO rd_know_rep

rename BJ DV_RR_1
rename Q72 DV_RR_Qualitative
rename BP DV_RR_2
rename BQ DV_RR_3
rename BV DV_RD_1
rename Q73 DV_RD_Qualitative
rename CB DV_RD_2
rename CC DV_RD_3

rename Q52 ideology
label variable ideology "higher = more conservative"

gen ideology_r = .
replace ideology_r = 1 if ideology == 7
replace ideology_r = 2 if ideology == 6
replace ideology_r = 3 if ideology == 5
replace ideology_r = 4 if ideology == 4
replace ideology_r = 5 if ideology == 3
replace ideology_r = 6 if ideology == 2
replace ideology_r = 7 if ideology == 1

gen FT = .
replace FT = Q18_5 - Q18_4 if rep_condition == 1
replace FT = Q18_4 - Q18_5 if dem_condition == 1

tab FT
*bottom quartile is 8; top quartile is 72

*adjust DV_RR_1 and DV_RD_1 to a 0-10 (as opposed to 1-11) scale
rename DV_RD_1 DV_RD_1_unadjusted
rename DV_RR_1 DV_RR_1_unadjusted
gen DV_RD_1 = DV_RD_1_unadjusted -1
gen DV_RR_1 = DV_RR_1_unadjusted -1

*demographic variables: using data collected in survey

rename Q182 age

*white non hispanic dummy variable
gen white_nh = 0
replace white_nh = 1 if Q184 == 2 & Q185 == "1"
replace white_nh = . if Q184 > 1000 | Q185 == "."

*gender dummy - 1 = male
gen male = .
replace male = 1 if Q183 == 1
replace male = 0 if Q183 != 1 & Q183 < 1000

*education variable
rename Q186 education

**awareness variable - awareness_p is based on work of PE Jones. See here:
*https://www.pejones.org/publication/2022_jop/

*variables 1-5 are knowledge variables, and 6-7 are interest variables. 1-5
*are coded so that 1 = correct answer, 0 otherwise. 6 and 7 are scaled 0-1, where
*1 indicates greatest interest

gen a1 = 0
gen a2 = 0
gen a3 = 0
gen a4 = 0
gen a5 = 0
gen a6  = 0
gen a7 = 0

replace a1 = 1 if know_1 == 2
replace a1 = 1 if know_1 > 1000
replace a2 = 1 if know_2 == 3
replace a2 = 1 if know_2 > 1000
replace a3 = 1 if know_3 == 3
replace a3 = 1 if know_3 > 1000
replace a4 = 1 if know_4 == 2
replace a4 = 1 if know_4 > 1000
replace a5 = 1 if know_5 == 2
replace a5 = 1 if know_5 > 1000

replace a6 = 1 if Q112 == 1
replace a6 = .75 if Q112 == 2
replace a6 = .5 if Q112 == 3
replace a6 = .25 if Q112 == 4
replace a6 = 0 if Q112 == 5
replace a6 = . if Q112 > 1000

replace a7 = 1 if Q113 == 1
replace a7 = .5 if Q113 == 2
replace a7 = 0 if Q113 == 3
replace a7 = . if Q113 > 1000

gen awareness_add = a1 + a2 + a3 + a4 + a5 + a6 + a7
tab awareness_add

gen awareness_p = .
replace awareness_p = 1.59  if awareness_add == 0
replace awareness_p = 2.12  if awareness_add == .25
replace awareness_p =  2.29 if awareness_add == .5
replace awareness_p =  2.82 if awareness_add == .75
replace awareness_p =  4.24 if awareness_add == 1
replace awareness_p =  6.09 if awareness_add == 1.25
replace awareness_p =  6.8 if awareness_add == 1.5
replace awareness_p =  7.59 if awareness_add == 1.75
replace awareness_p =  11.03 if awareness_add == 2
replace awareness_p = 15.00 if awareness_add == 2.25
replace awareness_p =  16.68 if awareness_add == 2.5
replace awareness_p = 19.06 if awareness_add == 2.75
replace awareness_p = 23.39  if awareness_add == 3
replace awareness_p = 27.89  if awareness_add == 3.25
replace awareness_p =  29.13 if awareness_add == 3.5
replace awareness_p =  34.33 if awareness_add == 3.75
replace awareness_p =  43.07 if awareness_add == 4
replace awareness_p = 50.66 if awareness_add == 4.25
replace awareness_p = 51.90  if awareness_add == 4.5
replace awareness_p =  58.16 if awareness_add == 4.75
replace awareness_p = 66.73 if awareness_add == 5
replace awareness_p = 73.43  if awareness_add == 5.25
replace awareness_p = 74.23  if awareness_add == 5.5
replace awareness_p =  81.20 if awareness_add == 5.75
replace awareness_p = 88.88 if awareness_add == 6
replace awareness_p = 91.97  if awareness_add == 6.25
replace awareness_p =  92.5 if awareness_add == 6.5
replace awareness_p =  95.41 if awareness_add == 6.75
replace awareness_p =  100 if awareness_add == 7

gen awareness_pp = awareness_p/100
drop awareness_p
rename awareness_pp awareness_p

*GEI variable

alpha Q54 Q55 Q56 Q57, std 
*alpha of 0.88
factor Q54 Q55 Q56 Q57, ml
rotate, varimax
predict GEI_factor, regression

egen GEI_min = min(GEI_factor)
gen GEI_factor_final2 = GEI_factor-GEI_min
egen GEI_max = max(GEI_factor_final2)
gen GEI_factor_final = GEI_factor_final2/GEI_max
drop GEI_factor
rename GEI_factor_final GEI_factor
*this creates a GEI variable based on factor scores for the four items,
*rescaled to 0 through 1

*individual empathy index
gen Q79_r = .
replace Q79_r = 1 if Q79 == 5
replace Q79_r = 2 if Q79 == 4
replace Q79_r = 3 if Q79 == 3
replace Q79_r = 4 if Q79 == 2
replace Q79_r = 5 if Q79 == 1

gen ind_empathy = Q75 + Q78 + Q79_r

***analysis

*individual empathy table (table 2)
 eststo clear
 drop flag
 gen flag = 0 
replace flag = 1 if rep_condition == 1 & persp == 1
replace flag = 1 if rep_condition == 1 & info == 1
regress ind_empathy persp white_nh male age education ideology_r if flag == 1, vce(robust)
est sto m1

 eststo clear
 drop flag
 gen flag = 0 
replace flag = 1 if dem_condition == 1 & persp == 1
replace flag = 1 if dem_condition == 1 & info == 1
regress ind_empathy persp white_nh male age education ideology_r if flag == 1, vce(robust)
est sto m2

esttab m1 m2 using table.rtf, b(2) se(2) r2 aic bic replace

*figure 3

eststo clear
 drop flag
 gen flag = 0 
replace flag = 1 if rep_condition == 1 
regress DV_RR_1 persp info white_nh male age education ideology_r if flag == 1, vce(robust)
est sto m1
margins, dydx(persp info ideology_r)  post
est sto f1

 drop flag
 gen flag = 0 
replace flag = 1 if dem_condition == 1 
regress DV_RD_1 persp info white_nh male age education ideology_r if flag == 1, vce(robust)
est sto m2
margins, dydx(persp info ideology_r)  post
est sto f2

coefplot (f1, label(Republicans) mcolor(red) msymbol(circle_hollow) ciopts(lcol(red))) ///
 (f2 , label(Democrats) mcolor(blue) msymbol(triangle_hollow) ciopts(lcol(blue))), ///
   scheme(s1color) vertical yline(0, lpattern(dash)) ///
 ytitle("Predicted Treatment Effect (0-10 Scale)") ///
	xlabel(1 "Perspective Getting Condition" 2 "Informational Condition" 3 "Ideology", labsize(small)) ///
	note("N = 504 in Republican vignette, 627 in Democratic vignette." "Estimates created using OLS. Results consistent with ordered logit model.", size(small)) 
	
graph export "C:\Users\Ryan\Documents\summer 2024\Perspective Taking Paper\figures\Fig4_randr.png", as(png) width(4000) replace

*figure 4

  eststo clear
drop flag
gen flag = 0 
replace flag = 1 if dem_condition == 1 
regress DV_RD_1 i.persp c.FT i.persp#c.FT info white_nh male age education ideology_r if flag == 1, vce(robust)
margins, at(FT=(8 72) persp=(0 1) info=0)
margins, at(FT=(0(5)100) persp=(0 1) info=0)
	marginsplot,  ytitle("") xtitle("Affective Partisanship")  ///
	scheme(s1color) xlabel(8 "Bottom Quartile" 40 "Median" 72 "Top Quartile", labsize(small)) ///
	ylabel(3 "3 - More Unfair" 4 5 6 "6 - More Fair", angle(30) labsize(small)) legend(label(7 "Control") label(2 "Perspective Taking")) ///
	note("N = 623. Estimates created using OLS.", size(small))

	
	
	
	